export type CaseCondition<T> = T extends Function ? never : ((t: T) => boolean) | T
export type Case<T, R> = [CaseCondition<T>, (t: T) => R];

export function when<T>(value: T): <R>(...cases: Case<T, R>[]) => R {
    return <R>(...cases: Case<T, R>[]): R => {
        for (const [is, run] of cases) {
            if (value === is || (typeof is == "function" && is(value))) {
                return run(value);
            }
        }
        throw new Error("非详尽");
    };
}

export default when;